﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.EmployeeModules
{
    public class EmployeeUpdaterRegister
    {
        int rows;
        UserProfile userProfile;
        public EmployeeUpdaterRegister()
        {
            userProfile = Authority.Authority.GetUserProfile();
        }
        public NormalDataUpdater<EmployeeModel> GetEmployeeUpdater()
        {
            NormalDataUpdater<EmployeeModel> systemDataUpdater = new NormalDataUpdater<EmployeeModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += TitleValid;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<EmployeeModel> e)
        {
            string message;
            SalesPointEmployeeFetcher salesPointFetcher = new SalesPointEmployeeFetcher();
            OrderFetcher orderFetcher = new OrderFetcher();
            PayFetcher payFetcher = new PayFetcher();
            DeliveryFetcher deliveryFetcher = new DeliveryFetcher();
            ServiceOrderFetcher serviceOrderFetcher = new ServiceOrderFetcher();
            ServiceBonusFetcher serviceBonusFetcher = new ServiceBonusFetcher();
            SalesBonusFetcher salesBonusFetcher = new SalesBonusFetcher();
            DesignerBonusFetcher designerBonusFetcher = new DesignerBonusFetcher();
            SalesPointEmployeeModel salesPointEmployeeModel = salesPointFetcher.GetByEmployee(e.Delete.Site, e.Delete.AutoId, out message);
            if (salesPointEmployeeModel != null)
            {
                e.Message = String.Format("档案{0}已被门店职员{1}使用",e.Delete.Code, salesPointEmployeeModel.EmployeeId);
                e.Cancle = true;
                return;
            }

            if(orderFetcher.ListBySalesId(e.Delete.Site,e.Delete.AutoId,out message, out rows).Count>0)
            {
                e.Message = String.Format("档案{0}已被订单使用", e.Delete.Code);
                e.Cancle = true;
                return;
            }
            if (payFetcher.ListByAccounter(e.Delete.Site, e.Delete.AutoId, out message, out rows).Count > 0)
            {
                e.Message = String.Format("档案{0}已被付款档案使用", e.Delete.Code);
                e.Cancle = true;
                return;
            }
            if (deliveryFetcher.ListByCourier(e.Delete.Site, e.Delete.AutoId, out message, out rows).Count > 0)
            {
                e.Message = String.Format("档案{0}已被物流档案使用", e.Delete.Code);
                e.Cancle = true;
                return;
            }
            if (serviceOrderFetcher.ListByCustomService(e.Delete.Site, e.Delete.AutoId, out message, out rows).Count > 0)
            {
                e.Message = String.Format("档案{0}已被服务单使用", e.Delete.Code);
                e.Cancle = true;
                return;
            }
            if (serviceBonusFetcher.ListByCustomService(e.Delete.Site, e.Delete.AutoId, out message, out rows).Count > 0)
            {
                e.Message = String.Format("档案{0}已被服务奖金档案使用", e.Delete.Code);
                e.Cancle = true;
                return;
            }
            if (salesBonusFetcher.ListSales(e.Delete.Site, e.Delete.AutoId, out message, out rows).Count > 0)
            {
                e.Message = String.Format("档案{0}已被销售奖金档案使用", e.Delete.Code);
                e.Cancle = true;
                return;
            }
            if (designerBonusFetcher.ListByDesigner(e.Delete.Site, e.Delete.AutoId, out message, out rows).Count > 0)
            {
                e.Message = String.Format("档案{0}已被设计奖金档案使用", e.Delete.Code);
                e.Cancle = true;
                return;
            }
        }

        private void SingletonValid(object sender, DataValidationEventArgs<EmployeeModel> e)
        {
            EmployeeFetcher employeeFetcher = new EmployeeFetcher();
            string message = "";
             
            EmployeeModel employee = employeeFetcher.GetEmployeeByCode( userProfile.SiteId, e.Insert.Code, out message);
            if (employee != null && employee.AutoId != e.Insert.AutoId)
            {
                e.Cancle = true;
                message = String.Format("代号[{0}]已被[{1}]占用", employee.Code, employee.Name);
                return;
            }
        }

        private void DataNotSpaceValid(object sender, DataValidationEventArgs<EmployeeModel> e)
        {
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Message = "代号不能为空";
                e.Cancle = true;
                return;
            }
            if (String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Message = "名称不能为空";
                e.Cancle = true;
                return;
            }
        }
        private void TitleValid(object sender, DataValidationEventArgs<EmployeeModel> e)
        {
            TitleFetcher titleFetcher = new TitleFetcher();
            string message;
            if(titleFetcher.Get(e.Insert.Site,e.Insert.TitleId,out message)==null)
            {
                e.Message = String.Format("职等档案有误,无法找到{0}的职等档案",e.Insert.TitleId);
                e.Cancle = true;
                return;
            }
        }
    }
}
